package com.cdck.safe.dete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdck.safe.dete.common.enums.CommonStatusEnum;
import com.cdck.safe.dete.model.entity.Activity;
import com.cdck.safe.dete.mapper.ActivityMapper;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.service.ActivityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Activity)表服务实现类
 *
 * @author wp
 * @since 2024-02-26 22:57:18
 */
@Service
@RequiredArgsConstructor
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    /**
     * 通过ID查询单条数据
     *
     * @param activityId 主键
     * @return 实例对象
     */
    @Override
    public Activity getActivityFormData(Long activityId) {
        return this.getById(activityId);
    }

    /**
     * 分页查询
     * queryParams 筛选条件
     * @return 查询结果
     */
    @Override
    public IPage<Activity> queryByPage(BaseKeywordsQuery queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        Page<Activity> page = new Page<>(pageNum, pageSize);
        return this.page(
                page,
                new LambdaQueryWrapper<Activity>()
                        .like(Objects.nonNull(keywords), Activity::getActivityName, keywords)
                        .eq(Objects.nonNull(queryParams.getStudent()) && queryParams.getStudent(),Activity::getStatus, CommonStatusEnum.NORMAL_STATUS.getValue())
        );
    }

    /**
     * 新增数据
     *
     * @param activity 实例对象
     * @return 实例对象
     */
    @Override
    public boolean saveActivity(Activity activity) {
        return this.save(activity);
    }

    /**
     * 修改数据
     *
     * @param activity 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateActivity(Long id, Activity activity) {
        return this.updateById(activity);
    }

    /**
     * 通过主键删除数据
     *
     * @param idsStr ids集合
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteActivitys(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    @Transactional
    public void addMember(String activityId) {
        Activity byId = this.getById(activityId);
        if (Objects.nonNull(byId)){
            byId.setMembers(byId.getMembers() + 1);
            this.saveOrUpdate(byId);
        }
    }

    @Override
    @Transactional
    public void removeMembers(List<Long> ids) {
        if (!ids.isEmpty()){
            Activity byId = this.getById(ids.get(0));
            if (Objects.nonNull(byId)){
                byId.setMembers(byId.getMembers() - ids.size());
                this.saveOrUpdate(byId);
            }
        }
    }
}
